/*
 * 
 * 
 * 
 */
package com.hboxs.asl.service.impl;

import com.hboxs.asl.dao.VerificationCodeDao;
import com.hboxs.asl.entity.VerificationCode;
import com.hboxs.asl.service.VerificationCodeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import javax.persistence.LockModeType;
import java.util.Date;

/**
 * Service - 邮箱验证码
 */
@Service("verificationCodeServiceImpl")
public class VerificationCodeServiceImpl extends BaseServiceImpl<VerificationCode, Long> implements VerificationCodeService {

    @Resource(name = "verificationCodeDaoImpl")
    private VerificationCodeDao verificationCodeDao;

    @Resource(name = "verificationCodeDaoImpl")
    public void setBaseDao(VerificationCodeDao verificationCodeDao) {
        super.setBaseDao(verificationCodeDao);
    }
    public boolean phoneExists(String phone) {
        return verificationCodeDao.phoneExists(phone);
    }
    @Transactional(readOnly = true)
    public VerificationCode findByPhone(String phone) {
        return verificationCodeDao.findByPhone(phone);
    }

    public void save(VerificationCode verificationCode) {
        Assert.notNull(verificationCode);
        verificationCodeDao.persist(verificationCode);
    }
    public void update(VerificationCode verificationCode, String code, Date date){
        Assert.notNull(verificationCode);

        verificationCodeDao.lock(verificationCode, LockModeType.PESSIMISTIC_WRITE);

        verificationCode.setCode(code);
        verificationCode.setSendDate(date);
        verificationCodeDao.merge(verificationCode);
    }

}
